
window.onload = function (){
    initUser();
    findByItem(1);
}

/**
 * 初始化用户信息
 */
function initUser(){
    axios.get("/user/getLoginUser").then(resp =>{
        var info = resp.data;
        if(info == ""){
            $("userDiv").innerHTML = "你好，请先<a href='/login.html'>登陆</a>";
        }
        else{
            $("userDiv").innerHTML = "欢迎光临，"+info.nickName+
                "<span style='margin-left: 100px;cursor: pointer' onclick='exitLogin()'>安全退出</span>";
        }
    })
}

function exitLogin(){
    axios.get("/user/exitLogin").then(resp =>{
       if(resp.data == "ok"){
           initUser();
       }
    });
}

/**
 * 动态条件分页查询商品
 * @param pageNO 页码
 */
function findByItem(pageNO){
    axios.get("/product/findByItem",{
        params: {
            pageNO,
            name:$("name").value
        }
    }).then(resp =>{
        var info = resp.data;
        var str = "";
        info.records.forEach(n =>{
           str += `<div style="width: 300px;height: 300px;text-align: center">
                <img src="/productImg/${n.imgPath}" width="200" height="200"><br>
                商品名：${n.name}<br>
                单价：${n.price}<br>
                评论数量：${n.infoNum}<br>
                <input type="button" value="查看" onclick="location.href='/info.html?id=${n.id}'">
            </div>`;
        });
        $("productDiv").innerHTML = str;

        var cutStr = "";
        for (var i=1;i<=info.pages;i++){
            cutStr += `<a style="margin-left: 10px" href="javascript:findByItem(${i})">${i}</a>`;
        }
        $("cutDiv").innerHTML = cutStr;
    })
}